﻿using System;
using System.Linq;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Exceptions;
using WmsApp.Factory;
using WmsApp.Lookup;
using WmsApp.StaticData;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.UI.DataBinding
{
    public class Binding
    {
        public static void BindProvider(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OWNERNO", "代码"),
                                                               new LookUpColumnInfo("OWNERNAME", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OWNERNAME";
            itemLookUpEdit.Name = "LookUpEditProvider";
            itemLookUpEdit.Properties.ValueMember = "OWNERNO";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetCustomers();
            var c = new Customer {OWNERNO = "", OWNERNAME = ""};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }

        public static void BindInStoreType(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("InStoreCode", "代码"),
                                                               new LookUpColumnInfo("InStoreName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "InStoreName";
            itemLookUpEdit.Name = "LookUpEditInStoreType";
            itemLookUpEdit.Properties.ValueMember = "InStoreCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetInStoreTypes();
            var c = new InStoreType {InStoreCode = "", InStoreName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }

        public static void BindOutStoreType(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.Clear();
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OutStoreCode", "代码"),
                                                               new LookUpColumnInfo("OutStoreName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OutStoreName";
            itemLookUpEdit.Name = "LookUpEditOutStoreType";
            itemLookUpEdit.Properties.ValueMember = "OutStoreCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetOutStoreTypes();
            var c = new OutStoreType {OutStoreCode = "", OutStoreName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }

        /// <summary>
        ///   必须在配置文件中的
        /// </summary>
        /// <param name = "lookUpEdit"></param>
        public static void BindOutStoreTypeInConfig(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.Clear();
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("OutStoreCode", "代码"),
                                                               new LookUpColumnInfo("OutStoreName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "OutStoreName";
            itemLookUpEdit.Name = "LookUpEditOutStoreType";
            itemLookUpEdit.Properties.ValueMember = "OutStoreCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetOutStoreTypesInConfig();
            itemLookUpEdit.Properties.DataSource = list;
            if (list.Count > 0)
            {
                itemLookUpEdit.EditValue = list[0].OutStoreCode;
            }
        }

        public static void BindingBillState(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("Code", "单据类型代码"),
                                                               new LookUpColumnInfo("Name", "单据类型名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "Name";
            itemLookUpEdit.Name = "LookUpEditBillState";
            itemLookUpEdit.Properties.ValueMember = "Code";
            itemLookUpEdit.Properties.NullText = "";
            var billState = new BillState {Code = "", Name = ""};
            var list = BillState.GetBillStateList();
            var tempList = list.ToList();
            tempList.Insert(0, billState);
            itemLookUpEdit.Properties.DataSource = tempList;
        }

        public static void BindingGoodsCodeType(LookUpEdit lookUpEdit)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;
                itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
                itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                               {
                                                                   new LookUpColumnInfo(
                                                                       "NewGoodsCode", "新物料代码", 120),
                                                                   new LookUpColumnInfo(
                                                                       "NewGoodsName", "新物料名称", 160),
                                                                   new LookUpColumnInfo(
                                                                       "OldGoodsCode", "老物料代码", 120),
                                                                   new LookUpColumnInfo(
                                                                       "StoreLocation", "库位", 100)
                                                               });
                itemLookUpEdit.Properties.DisplayMember = "NewGoodsCode";
                itemLookUpEdit.Name = "LookUpEditGoodsCodeType";
                itemLookUpEdit.Properties.ValueMember = "NewGoodsCode";
                itemLookUpEdit.Properties.NullText = "";
                itemLookUpEdit.Properties.PopupFormWidth = 500;
                var goodsCodeType = new GoodsCodeType {NewGoodsCode = "", NewGoodsName = "", OldGoodsCode = ""};
                //List<GoodsCodeType> list = Factory.WmsServiceFactory.GetServiceClient().GetGoodsCodeTypes();
                var list = StaticObjects.GetGoodsCodeTypes().ToList();
                list.Insert(0, goodsCodeType);
                itemLookUpEdit.Properties.DataSource = list;
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        public static void BindingGoodsCodeType(GridLookUpEdit lookUpEdit)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;

                var gridView = new GridView();
                gridView.Columns.AddRange(new[]
                                              {
                                                  new GridColumn
                                                      {
                                                          FieldName = "NewGoodsCode",
                                                          Name = "新物料代码",
                                                          Caption = @"新物料代码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 0
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "NewGoodsName",
                                                          Name = "新物料名称",
                                                          Caption = @"新物料名称",
                                                          Width = 160,
                                                          Visible = true,
                                                          VisibleIndex = 1
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "OldGoodsCode",
                                                          Name = "老物料代码",
                                                          Caption = @"老物料代码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 2
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "StoreTypeCode",
                                                          Name = "存货代码",
                                                          Caption = @"存货代码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 3
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "StoreTypeName",
                                                          Name = "存货名称",
                                                          Caption = @"存货名称",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 4
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "UnitCode",
                                                          Name = "单位代码",
                                                          Caption = @"单位代码",
                                                          Width = 80,
                                                          Visible = true,
                                                          VisibleIndex = 5
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "UnitName",
                                                          Name = "单位名称",
                                                          Caption = @"单位名称",
                                                          Width = 80,
                                                          Visible = true,
                                                          VisibleIndex = 6
                                                      }
                                              });
                gridView.FocusRectStyle = DrawFocusRectStyle.RowFocus;
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
                gridView.OptionsView.ShowAutoFilterRow = true;
                gridView.OptionsView.ShowGroupPanel = false;
                itemLookUpEdit.Properties.PopupFormWidth = 800;
                itemLookUpEdit.Properties.View = gridView;
                itemLookUpEdit.Properties.DisplayMember = "NewGoodsCode";
                itemLookUpEdit.Properties.Name = "repositoryItemLookUpEdit";
                itemLookUpEdit.Properties.ValueMember = "NewGoodsCode";
                itemLookUpEdit.Properties.NullText = "";

                var dataSourceList = StaticObjects.GetGoodsCodeTypes().ToList();
                dataSourceList.Insert(0, new GoodsCodeType {NewGoodsCode = "", NewGoodsName = "", OldGoodsCode = ""});

                itemLookUpEdit.Properties.DataSource = dataSourceList;
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        public static void BindStoreType(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("StoreCode", "代码"),
                                                               new LookUpColumnInfo("StoreName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "StoreName";
            itemLookUpEdit.Name = "LookUpEditStoreType";
            itemLookUpEdit.Properties.ValueMember = "StoreCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetStoreTypes();
            var c = new StoreType {StoreCode = "", StoreName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }

        public static void BindStoreType(LookUpEdit lookUpEdit, TextEdit valueTextEdit, string propertyName)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("StoreCode", "代码"),
                                                               new LookUpColumnInfo("StoreName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "StoreCode";
            itemLookUpEdit.Name = "LookUpEditStoreType";
            itemLookUpEdit.Properties.ValueMember = "StoreCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetStoreTypes();
            var c = new StoreType {StoreCode = "", StoreName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var storeType =
                                                            (StoreType)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (storeType != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty(propertyName);
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            storeType.StoreName, null);
                                                            }
                                                        }
                                                    };
        }

        public static void BindUnit(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("UnitCode", "代码"),
                                                               new LookUpColumnInfo("UnitName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "UnitName";
            itemLookUpEdit.Name = "LookUpEditUnit";
            itemLookUpEdit.Properties.ValueMember = "UnitCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetUnits();
            var c = new Unit {UnitCode = "", UnitName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }

        public static void BindUnit(LookUpEdit lookUpEdit, TextEdit valueTextEdit, string propertyName)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("UnitCode", "代码"),
                                                               new LookUpColumnInfo("UnitName", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "UnitCode";
            itemLookUpEdit.Name = "LookUpEditUnit";
            itemLookUpEdit.Properties.ValueMember = "UnitCode";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetUnits();
            var c = new Unit {UnitCode = "", UnitName = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;

            itemLookUpEdit.EditValueChanging += (sender, e) =>
                                                    {
                                                        var unit =
                                                            (Unit)
                                                            itemLookUpEdit.Properties.GetDataSourceRowByKeyValue(
                                                                e.NewValue);
                                                        if (unit != null)
                                                        {
                                                            if (itemLookUpEdit.DataBindings.Count > 0)
                                                            {
                                                                var type =
                                                                    itemLookUpEdit.DataBindings[0].DataSource.GetType();
                                                                var pi = type.GetProperty(propertyName);
                                                                pi.SetValue(itemLookUpEdit.DataBindings[0].DataSource,
                                                                            unit.UnitName, null);
                                                            }
                                                        }
                                                    };
        }

        public static void BindStoreGoodsState(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("Name", "状态")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "Name";
            itemLookUpEdit.Name = "LookUpEditProvider";
            itemLookUpEdit.Properties.ValueMember = "Code";
            itemLookUpEdit.Properties.NullText = "";
            var list = StoreGoodsState.GetStoreGoodsStateList();
            itemLookUpEdit.Properties.DataSource = list;
        }

        public static void BindingSystemUser(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("USERID", "代码"),
                                                               new LookUpColumnInfo("USERNAME", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "USERNAME";
            itemLookUpEdit.Name = "LookUpEditSystemUser";
            itemLookUpEdit.Properties.ValueMember = "USERID";
            itemLookUpEdit.Properties.NullText = "";
            var list = WmsServiceFactory.GetServiceClient().GetSystemUsers();
            var c = new SystemUser {USERID = "", USERNAME = "", ISVALID = Constant.IsValidTrue};
            list.Insert(0, c);
            itemLookUpEdit.Properties.DataSource = list;
        }
    }
}